package org.black.phoneUtility.sms.reply.content.broadcastReceiver;

import org.black.phoneUtility.sms.reply.app.service.SmsReplyNotificationIntentService;
import org.black.phoneUtility.sms.reply.provider.SmsReplyContract;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;

public class SentSmsBroadcastReceiver extends BroadcastReceiver {

	public static final String ARGUMENTS_SMS_TEXT = "intentSmsText";
	public static final String ARGUMENTS_ACTIVATIOIN_RECORD_ID = "activationRecordId";
	public static final String ARGUMENTS_DESTINATION_ADDRESS = "intentSmsDestinationAddress";
	public static final String ARGUMENTS_SOURCE_FROM_TYPE = "sourceFromType";

	private static final String LOG_TAG = SentSmsBroadcastReceiver.class
			.getName();

	@Override
	public void onReceive(Context context, Intent intent) {
		int resultCode = getResultCode();
		Log.d(LOG_TAG, "result code : " + resultCode);
		Uri data = intent.getData();
		String destinationAddress = intent
				.getStringExtra(ARGUMENTS_DESTINATION_ADDRESS);
		String smsMessageText = intent.getStringExtra(ARGUMENTS_SMS_TEXT);
		UpdateReplyHistoryThread writeReplyHistoryThread = new UpdateReplyHistoryThread(
				context, data, resultCode);
		writeReplyHistoryThread.start();
		switch (resultCode) {
		case Activity.RESULT_OK: {
			Intent updateNotificationServiceIntent = new Intent(context,
					SmsReplyNotificationIntentService.class);
			Bundle arguments = new Bundle();
			arguments
					.putInt(SmsReplyNotificationIntentService.ARGUMENTS_ACTION_TYPE,
							SmsReplyNotificationIntentService.ACTION_TYPE_SMS_REPLY_MESSAGE_BE_SENT);
			arguments.putString(
					SmsReplyNotificationIntentService.ARGUMENTS_SMS_MESSAGE,
					smsMessageText);
			arguments
					.putString(
							SmsReplyNotificationIntentService.ARGUMENTS_SMS_DESTINATION_ADDRESS,
							destinationAddress);
			updateNotificationServiceIntent.putExtras(arguments);
			context.startService(updateNotificationServiceIntent);
			Log.d(LOG_TAG, "Start SmsReplyNotificationIntentService!");
			break;
		}
		case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
			Log.d(LOG_TAG, "Generic Fail!");
			break;
		case SmsManager.RESULT_ERROR_NO_SERVICE:
			Log.d(LOG_TAG, "No SMS Service");
			break;
		case SmsManager.RESULT_ERROR_NULL_PDU:
			Log.d(LOG_TAG, "PDU Error!");
			break;
		case SmsManager.RESULT_ERROR_RADIO_OFF:
			Log.d(LOG_TAG, "Radio Off!");
			break;
		}

	}

	private class UpdateReplyHistoryThread extends Thread {
		private Context context;
		private Uri uri;
		private int resultCode;

		public UpdateReplyHistoryThread(Context context, Uri uri, int resultCode) {
			this.context = context;
			this.uri = uri;
			this.resultCode = resultCode;
		}

		@Override
		public void run() {
			if (uri != null) {
				ContentValues replyHistoryContentValues = new ContentValues();
				replyHistoryContentValues.put(
						SmsReplyContract.ReplyHistories.REPLY_STATUS,
						resultCode);
				context.getContentResolver().update(uri,
						replyHistoryContentValues, null, null);
			}

		}
	}

}
